<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/8
 * Time: 11:01
 */

namespace app\api\behavior;


use app\api\model\Behavior;
use app\api\model\User;
use app\server\controller\Push;

class ProductBehavior extends BaseBehavior
{
    public function actionBegin(&$params){
        $uid = $params['uid'];
        $info = explode('_',$params['wu_id']);
        $wu_id = $info[1];
        $url = request()->url();
        $userInfo = User::getUserInfoByUid($uid);
        $content = $params['msg'];
        $data['user_id'] = $uid;
        $data['url'] = $url;
        $data['content'] = $content;
        $data['type'] = 1;
        $data['wu_id'] = $wu_id;
        $data['weight'] = 2;
        $behavior = new Behavior();
        $result = $behavior->createBehavior($data);

        //计算成交率
        $rate = new \app\api\service\Behavior();
        $array =[ 'function'=>'rate','args'=>['behavior'=>'card_shopping','result'=>$result->toArray(),'wu_id'=>$wu_id]];
        $rate->asyncRate($array);

        //推送结果
        $news = $this->getMassgeInfo($userInfo,$result);
        $push = new Push($wu_id);
        $push->sendToClient($news);

        return $result;
    }
}